package com.lvandroid.transport.download;

import android.util.Log;

import com.arialyy.annotations.Download;
import com.arialyy.aria.core.Aria;
import com.arialyy.aria.core.task.DownloadTask;
import com.lvandroid.transport.task.DownLoadTaskInfo;
import com.lvandroid.transport.task.IOnDownloadTaskUpdateListener;
import com.lvandroid.transport.task.TaskInfo;

/**
 * Https协议下载器
 */
public class HttpsDownLoader implements IDownLoad {
    private static final String TAG = "HttpsDownLoader";

    private IOnDownloadTaskUpdateListener mTaskListener;

    public HttpsDownLoader() {
    }

    @Override
    public long start(String url, String filePath) {
        long id = Aria.download(this).load(url).setFilePath(filePath).create();
        Log.i(TAG, "start download url : " + url + ", save to : " + filePath + ", task id = " + id);
        return id;
    }

    @Override
    public boolean stop(long taskId) {
        Log.i(TAG, "stop download task id = " + taskId);
        Aria.download(this).load(taskId).stop();
        return false;
    }

    @Override
    public boolean resume(long taskId, boolean reStart) {
        Log.i(TAG, "resume download task id = " + taskId);
        Aria.download(this).load(taskId).resume(reStart);
        return false;
    }

    @Override
    public void addDownLoadListener(IOnDownloadTaskUpdateListener l) {
        mTaskListener = l;
    }

    @Download.onTaskRunning protected void onTaskUpdate(DownloadTask task) {
        //任务下载更新
        Log.i(TAG, "onTaskUpdate : " + task.getDownloadEntity());
        if(null != mTaskListener) {
            TaskInfo info = new DownLoadTaskInfo(task);
            mTaskListener.onUpdate(info);
        }
    }

    @Download.onTaskComplete void onTaskComplete(DownloadTask task) {
        //在这里处理任务完成的状态
        Log.i(TAG, "onTaskComplete : " + task.getDownloadEntity());
        if(null != mTaskListener) {
            TaskInfo info = new DownLoadTaskInfo(task);
            mTaskListener.onComplete(info);
        }
    }

}
